package com.sendi.mobilework;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.sendi.mobilework.api.Api;
import com.sendi.mobilework.datamodel.Result;
import com.sendi.mobilework.global.GlobalContent;
import com.sendi.mobilework.util.HttpUtils;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

/**
 * Created by Administrator on 2017/3/22.
 */

public class LoginActivity extends Activity {
    private EditText et_name;
    private EditText et_psw;
    private RadioGroup mRadioGroup;
    private int mIdentity;

    private Api mApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        mApi= HttpUtils.getRetrofit(GlobalContent.LOGINSERVER).create(Api.class);
        initView();
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.et_user_name);
        et_psw = (EditText) findViewById(R.id.et_psw);
        mRadioGroup = (RadioGroup) findViewById(R.id.rg_select);
    }

    public void login(View view) {
        String name = et_name.getText().toString();
        String psw = et_psw.getText().toString();
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_manager://选中领导
                        mIdentity = 0;
                        break;
                    case R.id.rb_worker://选中员工
                        mIdentity = 1;
                        break;
                }
            }
        });

        Log.i("TAG", "login: "+mIdentity);
        mApi.login(mIdentity,name,psw).enqueue(new retrofit2.Callback<Result>() {
            @Override
            public void onResponse(retrofit2.Response<Result> response) {
                String result=response.body().getResult();
                if (response.isSuccess()){
                    Log.i("SENDI", "onResponse: "+response.body().getResult());
                    if (result.equals("1")){
                        Log.i("TAG", "onResponse: "+"next");
                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                        intent.putExtra("identity",result);
                        startActivity(intent);
                    }
                }
            }

            @Override
            public void onFailure(Throwable t) {

            }
        });
    }
}
